#region LGPL License
//********************************************************************
//	author:         Thomas "zeroflag" Kraemer
//	author email:   zeroflag@zeroflag.de
//	
//	Copyright (C) 2006-2008  Thomas "zeroflag" Kraemer
//	
//	license:	(LGPL)
//	
//		This library is free software; you can redistribute it and/or
//		modify it under the terms of the GNU Lesser General Public
//		License as published by the Free Software Foundation; either
//		version 2.1 of the License, or (at your option) any later version.
//
//		This library is distributed in the hope that it will be useful,
//		but WITHOUT ANY WARRANTY; without even the implied warranty of
//		MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//		Lesser General Public License for more details.
//
//		You should have received a copy of the GNU Lesser General Public
//		License along with this library; if not, write to the Free Software
//		Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
//		http://www.gnu.org/licenses/lgpl.html#TOC1
//
//*********************************************************************
#endregion LGPL License

using System;
using System.Collections.Generic;
using System.Text;

namespace zeroflag.Threading
{
	public class Foreach<T> : Task<T>
	{
		/// <summary>
		/// Create a new parallel Foreach task.
		/// foreach(T value in collection) task(value);
		/// </summary>
		/// <param name="collection">The values to iterate over.</param>
		/// <param name="task">The task to execute for each value.</param>
		public Foreach(IEnumerable<T> collection, Task<T>.RunHandle task)
		{
			foreach (T value in collection)
			{
				this.And(new Foreach<T>(task, value));
			}
		}

		/// <summary>
		/// Create a new parallel Foreach task for one iteration.
		/// foreach(T value in collection) task(value);
		/// </summary>
		/// <param name="value">The value for this iteration.</param>
		/// <param name="task">The task to execute for each value.</param>
		protected Foreach(Task<T>.RunHandle task, T value)
		{
			this.Handle = delegate()
			{
				task(value);
			};
		}
	}
}
